package cn.sczc.jh.client.controller.equipment;

import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.sczc.jh.client.base.AbstractController;
import cn.sczc.jh.client.entity.EquipmentPos;
import cn.sczc.jh.client.entity.Role;
import cn.sczc.jh.client.entity.User;
import cn.sczc.jh.client.exception.BusinessException;
import cn.sczc.jh.client.service.equipment.EquipmentPosService;

@RestController
@RequestMapping("/equipment/pos")
public class EquipmentPosController extends AbstractController {

	@Autowired
	private EquipmentPosService equipmentPosService;

	@Autowired
	private RestTemplate restTemplate;

	@Value("${jh.cloud.url}")
	private String cloudUrl;

	/**
	 * 获取用户列表
	 * 
	 * @param modelMap
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	public ResponseEntity<ModelMap> list(ModelMap modelMap, @RequestParam Map<String, Object> params) {
		@SuppressWarnings("unchecked")
		Page<EquipmentPos> page = (Page<EquipmentPos>) super.getPage(params);
		Page<EquipmentPos> data = equipmentPosService.getListByParams(page, params);
		return setSuccessModelMap(modelMap, data);
	}

	@RequestMapping(value = "", method = RequestMethod.POST)
	public ResponseEntity<ModelMap> saveOrUpdatePos(ModelMap modelMap, @RequestBody EquipmentPos pos) {
		int count = equipmentPosService.insertOrUpdate(pos);
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		pos.setTeamId(user.getTeamId());
		try {
			ResponseEntity<ModelMap> response = restTemplate
					.postForEntity("http://" + cloudUrl + "api/equipment/saveOrUpdatePos", pos, ModelMap.class);
			HttpStatus statusCode = response.getStatusCode();
			if (!statusCode.is2xxSuccessful()) {
				throw new BusinessException("同步云端失败！请手动同步");
			} else {
				ModelMap m = response.getBody();
				if ((int) m.get("httpCode") != 200) {
					throw new BusinessException("同步云端失败！请手动同步");
//					return setModelMap(modelMap,409, "同步云端失败！请手动同步");
				}
			}
		} catch (Exception e) {
			throw new BusinessException("同步云端失败！请手动同步");
//			return setModelMap(modelMap, 409, "同步云端失败！请手动同步");
		}
		return setSuccessModelMap(modelMap, count);
	}

	@RequestMapping(value = "", method = RequestMethod.DELETE)
	public ResponseEntity<ModelMap> delPos(ModelMap modelMap, @RequestBody EquipmentPos pos) {
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		pos.setTeamId(user.getTeamId());
		try {
			ResponseEntity<ModelMap> response = restTemplate
					.postForEntity("http://" + cloudUrl + "api/equipment/delPos", pos, ModelMap.class);
			HttpStatus statusCode = response.getStatusCode();
			if (!statusCode.is2xxSuccessful()) {
				throw new BusinessException("删除云端数据失败,请重试!");
			} else {
				ModelMap m = response.getBody();
				if ((int) m.get("httpCode") != 200) {
					throw new BusinessException("删除云端数据失败,请重试!");
				} else {
					int count = equipmentPosService.delPos(pos);
					return setSuccessModelMap(modelMap, count);
				}
			}
		} catch (Exception e) {
			throw new BusinessException("删除云端数据失败,请重试");
		}
	}

	@RequestMapping(value = "sync", method = RequestMethod.POST)
	public ResponseEntity<ModelMap> sync(ModelMap modelMap, @RequestBody EquipmentPos pos) {
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		pos.setTeamId(user.getTeamId());
		ResponseEntity<ModelMap> response = restTemplate
				.postForEntity("http://" + cloudUrl + "api/equipment/saveOrUpdatePos", pos, ModelMap.class);
		HttpStatus statusCode = response.getStatusCode();
		if (!statusCode.is2xxSuccessful()) {
			throw new BusinessException("同步云端失败,请重试！");
		} else {
			ModelMap m = response.getBody();
			if ((int) m.get("httpCode") != 200) {
				throw new BusinessException("同步云端失败,请重试！");
			}
		}
		return setSuccessModelMap(modelMap, null);
	}
}
